/**
 * Created with IntelliJ IDEA
 * Description:
 * User: Administrator
 * Data: 2023 - 08 - 24
 * Time: 18:56
 */
//牛客 判断一个链表是否为回文结构
public class Solution2 {
    public boolean isPail (ListNode head) {
        if(head == null) return false;
        ListNode slow = head;
        ListNode fast = head;
        while(fast != null && fast.next != null) {
            slow = slow.next;
            fast = fast.next.next;
        }
        slow = reverse(slow);//一般不要让头指针head动
        fast = head;
        while(slow != null) {
            if(fast.val == slow.val) {
                fast = fast.next;
                slow = slow.next;
            }else {
                return false;
            }
        }
        return true;
    }
    public ListNode reverse(ListNode head) {
        ListNode pre = null;
        ListNode cur = head;
        while(cur != null) {
            ListNode curNext = cur.next;
            cur.next = pre;
            pre = cur;
            cur = curNext;
        }
        return pre;
    }
}
